package com.xhh.nphm.web.controller.system;

import com.xhh.nphm.common.core.controller.BaseController;
import com.xhh.nphm.common.utils.file.FileTools;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.xml.rpc.ServiceException;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/system/mail")
public class MailGetAccessoryCtrl extends BaseController
{
    @Value("${download.backUrl}")
    private String downloadUrl;
    @Value("${download.rootDir}")
    private String rootDirs;
    private String imgSuffix = "jpg,jpeg,png,bmp,gif,webp";

    @RequestMapping("downloadDoc")
    public void downloadDoc(String wsCode, Integer index, HttpServletResponse response)
    {
        try
        {
            String dateStr = DateFormatUtils.format(new Date(), "yyyyMMddHHmmss");
            int idx = Integer.valueOf(index).intValue();
            response.setContentType("application/msword; charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + dateStr + ".doc");
            Service service = new Service();
            Call call = (Call)service.createCall();
            call.setTargetEndpointAddress(new URL(downloadUrl));
            call.setOperation("getAccessoryDoc");
            call.setMaintainSession(true);
            Object object = call.invoke(new Object[] { wsCode, Integer.valueOf(idx) });
            ServletOutputStream os = response.getOutputStream();
            byte[] bys = (byte[])object;
            os.write(bys);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 返回文件名列表
     * @param wsCode 流水号
     * @param index 根目录
     * @param mmap
     * @return
     */
    @RequestMapping("mailAccessory")
    public ModelAndView mailAccessory(String wsCode, String index, ModelMap mmap)
    {
        if(rootDirs.contains(index)) {
            try {
                String[] resultArr = FileTools.getAccessoryName(wsCode,index);
                List<String> accessoryNames = new ArrayList<>();
                for(int i=0;i<resultArr.length;i++) {
                    String suffix = resultArr[i].substring(resultArr[i].lastIndexOf(".")+1).toLowerCase();
                    if((imgSuffix).contains(suffix)) {
                        accessoryNames.add(resultArr[i]);
                    }
                }
                mmap.put("accessoryNames", accessoryNames);
            } catch (ServiceException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return new ModelAndView("system/accessory/mailAccessory");
    }

    /**
     * 根据文件名查看图片
     * @param wsCode 流水号
     * @param imgName 文件名称
     * @param index 根目录名称
     * @param response
     */
    @RequestMapping("lookImage")
    public void lookImage(String wsCode, String imgName, String index, HttpServletResponse response)
    {
        if(rootDirs.contains(index)) {
            response.setContentType("image/jpeg");
            try {
                OutputStream os = response.getOutputStream();
                byte[] bytes = FileTools.getAccessoryByName(wsCode,imgName,index);
                os.write(bytes);
                os.flush();
                os.close();
            } catch (ServiceException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    @RequestMapping("getImg")
    public void getImg(String wsCode, Integer index, HttpServletResponse response)
    {
        int idx = Integer.valueOf(index).intValue();
        response.setContentType("image/jpeg");
        response.setCharacterEncoding("utf-8");
        byte[] bys = null;
        try {
            OutputStream os = response.getOutputStream();
            Service service = new Service();
            Call call = (Call)service.createCall();
            call.setTargetEndpointAddress(new URL(downloadUrl));
            call.setOperation("getAccessoryImg");
            call.setMaintainSession(true);
            bys = (byte[])call.invoke(new Object[] { wsCode, Integer.valueOf(idx) });
            os.write(bys);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}